﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pay_My_Server.DB;
using System.Windows.Forms;

namespace Pay_My_Server.Connector
{
    public class ADOConnector
    {
        public PayMyClassDataContext dataContext;
        private Boolean connectionStatus;

        public ADOConnector()
        {
            this.Status = false;
            this.DataContext = new PayMyClassDataContext();
        }

        public string CnxString { get { return this.DataContext.Connection.ConnectionString ; } }

        public Boolean Status { get { return connectionStatus; } set { connectionStatus = value; } }

        public void Connect()
        {
            if (dataContext.DatabaseExists())
            {
                this.DataContext.Connection.Open();
                this.Status = true;
            }
            else
                MessageBox.Show("erreur de connexion avec : "+ dataContext.Connection.Database+"");
        }

        public void Disconnect()
        {
            if (dataContext.Connection != null)
            {
                this.DataContext.Connection.Close();
                this.Status = false;
            }
            else
                MessageBox.Show("erreur de deconnexion, adapter non connecté ?");
        }

        public void SaveOrUpdate()
        {
            if ( this.Status)
                this.DataContext.Transaction.Commit();
        }

        public PayMyClassDataContext DataContext { get { return dataContext; } set { dataContext = value; } }
    }
}
